package zhjb.temp.db;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 获取dao的工厂
 * 
 * @author zhangjianbing@msn.com
 * 
 */
public class DaoContext {
	private static ThreadLocal<Connection> connctions = new ThreadLocal<Connection>();

	public static Dao getDao() {
		return new MySQL();
	}

	/**
	 * 添加链接
	 * 
	 * @param conn
	 */
	public static void add(Connection conn) {
		connctions.set(conn);
	}

	/**
	 * 获取链接
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		if (connctions.get() == null) {
			connctions.set(DBInit.getDBInit().getNewConnection());
		}
		return connctions.get();
	}
	
	/**
	 * 關閉連接
	 */
	public static void close() {
		Connection conn = getConnection();
		connctions.remove();
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
